﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Reflection;
using IScript;

namespace PanTiltCruise
{
    public class CruiseBase : ICruiseAnalyzeScript
    {

        #region 私有变量
        Thread threadCruiseAnalyze;
        #endregion

        #region 私有方法
        /// <summary>
        /// 巡航识别线程
        /// </summary>
        void CruiseAnalyzeThread()
        {
            while (this.IsAnalyzing || this.IsCruising)
            {
                if (this.IsAnalyzing)
                {
                    this.AsyncGetHorizon();
                    Thread.Sleep(1000);
                    if (this.IsAnalyzing) this.AsyncGetVertical();
                    Thread.Sleep(1000);
                    if (this.IsAnalyzing) this.AnalyzeStrategy();
                }

                if (this.IsCruising)
                {
                    OnCruise();
                }

                Thread.Sleep(2000);
            }

            if (this.stopCallback != null) stopCallback();
        }

        void AsyncGetHorizon()
        {
            if (this.scriptCallFucntion != null)
                this.scriptCallFucntion("AsyncGetHorizon");
        }

        void AsyncGetVertical()
        {
            if (this.scriptCallFucntion != null)
                this.scriptCallFucntion("AsyncGetVertical");
        }

        void AnalyzeStrategy()
        {
            if (this.scriptCallFucntion != null)
                this.scriptCallFucntion("AnalyzeStrategy");
        }
        #endregion

        #region 虚方法
        protected virtual void OnStart()
        {

        }

        protected virtual void OnCruise()
        {

        }

        protected virtual void OnStop()
        {

        }
        #endregion

        #region ICruiseAnalyzeScript 成员
        public bool IsAnalyzing { get; set; }

        public bool IsCruising { get; set; }
        #endregion

        #region IScript 成员
        protected CallFunctionCallback scriptCallFucntion;
        public CallFunctionCallback ScriptCallFucntion
        {
            set { scriptCallFucntion = value; }
        }
        protected CallParameterCallback scriptCallParameter;
        public CallParameterCallback ScriptCallParameter
        {
            set { scriptCallParameter = value; }
        }

        StopCallback stopCallback;
        public StopCallback StopCallback
        {
            set { stopCallback = value; }
        }

        public bool IsStarted
        {
            get { return this.IsAnalyzing || this.IsCruising; }
        }

        public bool Start()
        {
            if (this.threadCruiseAnalyze == null || !this.threadCruiseAnalyze.IsAlive)
            {
                if (this.IsCruising) OnStart();
                this.threadCruiseAnalyze = new Thread(new ThreadStart(CruiseAnalyzeThread));
                this.threadCruiseAnalyze.Start();
            }

            return true;
        }

        public bool Stop()
        {
            if (this.threadCruiseAnalyze != null && this.threadCruiseAnalyze.IsAlive)
            {
                if (this.IsCruising) OnStop();

                this.IsAnalyzing = this.IsCruising = false;

                this.threadCruiseAnalyze.Abort();
            }

            return true;
        }

        public bool SetValue(string key, object value)
        {
            Type t = this.GetType();
            PropertyInfo pi = t.GetProperty(key);

            if (pi != null)
            {
                pi.SetValue(this, value, null);

                return true;
            }
            else
                return false;
        }

        public object GetValue(string key)
        {
            Type t = this.GetType();
            PropertyInfo pi = t.GetProperty(key);

            if (pi != null)
                return pi.GetValue(this, null);
            else
                return null;
        }

        #endregion
    }
}
